package com.hk.aefz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.aefz.mapper.SpecialMapper;
import com.hk.aefz.po.Special;
import com.hk.aefz.service.SpecialService;
import com.hk.aefz.vo.PageResult;
import com.hk.aefz.vo.ResultMsg;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 个人分类专栏 服务实现类
 * </p>
 *
 * @author 阿尔法哲
 * @since 2020-07-25
 */
@Service
public class SpecialServiceImpl extends ServiceImpl<SpecialMapper, Special> implements SpecialService {

    @Autowired
    private SpecialMapper specialMapper;



    @ApiOperation("分页查询当前账户下的分类/回收分类查询")
    @Override
    public PageResult<Special> selectAll(Integer page, Integer size,Integer dedid) {
        PageResult<Special> result = new PageResult<>();
        Page<Special> page1 = new Page<>(page,size);
        //        获取认证的账户id
        final int aid = (int) SecurityUtils.getSubject().getSession().getAttribute("aid");
        QueryWrapper<Special> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("aid",aid)
                .eq("sstate",0)
                .eq("dedid",dedid)
                .orderByAsc("sid");
        Page<Special> specialPage = specialMapper.selectPage(page1, queryWrapper);
        long total = specialPage.getTotal();
        long pages = 0l;
        if (total != 0l) {
            pages = total/size;
            if (total % size == 0) {
                pages = pages;
            }else {
                pages = pages+1;
            }
        }
        result.setObjects(specialPage.getRecords());
        result.setTotal(total);
        result.setSize(specialPage.getSize());
        result.setPages(pages);
        result.setCurrent(specialPage.getCurrent());
        return result;
    }

    @ApiOperation("根据分类id永久删除")
    @Override
    public ResultMsg deleteSpecial(Integer sid) {
        ResultMsg resultMsg = new ResultMsg();
        int deleteById = specialMapper.deleteById(sid);
        if (deleteById == 1) {
            resultMsg.setState(200);
            resultMsg.setMsg("该分类已永久删除!");
        } else {
            resultMsg.setState(500);
            resultMsg.setMsg("删除失败，请重新操作!");
        }
        return resultMsg;
    }

    @ApiOperation("新建个人分类")
    @Override
    public ResultMsg insertSpecial(Special special) {
        ResultMsg resultMsg = new ResultMsg();
//        进行封装
        int insert = specialMapper.insert(special);
        if (insert == 1) {
            resultMsg.setState(201);
            resultMsg.setMsg("新建成功!");
        } else {
            resultMsg.setState(202);
            resultMsg.setMsg("数据异常,稍后重试!");
        }
        return resultMsg;
    }

    @ApiOperation("根据分类id查询单独分类信息")
    @Override
    public Special selectBySid(Integer sid, Integer dedid) {
        QueryWrapper<Special> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sid",sid)
                .eq("dedid",dedid);
        List<Special> specials = specialMapper.selectList(queryWrapper);
        Special special = specials.get(0);
        return special;
    }

    @ApiOperation("根据分类id修改单独分类信息")
    @Override
    public ResultMsg updateSpecial(Special special) {
        ResultMsg resultMsg = new ResultMsg();
        int updateById = specialMapper.updateById(special);
        if (updateById == 1) {
            resultMsg.setState(201);
            resultMsg.setMsg("保存成功!");
        } else {
            resultMsg.setState(202);
            resultMsg.setMsg("数据异常,稍后重试!");
        }
        return resultMsg;
    }

    @Override
    public Special selectByEid(Integer eid) {
        Special sbids = specialMapper.selectByEid(eid);
        if (sbids == null) {
            System.out.println("搜索不到相关分类及标签");
        }
        return sbids;
    }
}
